<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Http\Models\Video
 *
 * @property int $id
 * @property int|null $type_id 分类id
 * @property string|null $type_name 分类名称
 * @property string|null $name 影视名称
 * @property string|null $img 图片
 * @property string|null $desc 影视描述
 * @property string|null $director 影视导演
 * @property string|null $performer 主演
 * @property string|null $url 影视地址
 * @property int|null $num 观看人数
 * @property int|null $fraction 分数
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int|null $isdelete -1冻结0删除1正常
 * @property int|null $param 其他参数
 * @property-read \App\Http\Models\Type|null $video_type
 * @method static \Illuminate\Database\Eloquent\Builder|Video newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Video newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Video query()
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereDirector($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereFraction($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereIsdelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereParam($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video wherePerformer($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereTypeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereTypeName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Video whereUrl($value)
 * @mixin \Eloquent
 */
class Video extends Model
{
    //
    protected $table = 'video';
    protected $hidden = [
        'param','isdelete','created_at','updated_at'
    ];

    /**
     * 影视列表
     * @param $param
     * @return mixed
     */
    public static function lists($param)
    {
        $list['data'] = self::where(function ($query) use($param){
            if (isset($param['type_id'])){
                $query->where('type_id',$param['type_id']);
            }
            if (isset($param['search'])){
                $query->where('name','like',"%p$param[search]%");
            }
        })->skip($param['page'])->take($param['limit'])->select('id','name','img')->inRandomOrder()->get();
        $list['count'] = count($list['data']);
        return $list;
    }

    public function getImgAttribute($value)
    {
        $http =  strpos($value,'http');
        $https = strpos($value,'https');
        if ($http === false && $https === false ){
            return env('APP_URL').'storage/uploads/'.$value;
        }else{
            return $value;
        }
    }

    /**
     * 连接影视分类表
     */
    public function video_type()
    {
       return  $this->hasOne('App\Http\Models\Type','id','type_id');
    }

}
